/*

Copyright 2009 Michel Boto. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the
distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#pragma once
#include <stack>
#include "Base.h"
#include "ChildIteratorHolder.h"

namespace Blatt
{

template <class DerivedType, class NodeType, class ChildIteratorType>
class PreOrder : public Base<DerivedType,NodeType>
{
public:
	PreOrder()
	: Base<DerivedType,NodeType>()
	{}
	
	PreOrder( NodeType& rootNode )
	: Base<DerivedType,NodeType>( rootNode )
	{
	}
	
protected:
	typedef ChildIteratorHolder<ChildIteratorType> TChildItHolder;
	
private:

	std::stack<TChildItHolder> _currentChildStack;
	
	friend class boost::iterator_core_access;
	
	
	void windBack( const bool incrementNext ) {
		_currentChildStack.pop();
		while ( !_currentChildStack.empty() )
		{
			// find the first "parent" which still has a valid iterator
			ChildIteratorHolder<ChildIteratorType>& nextChildRange = _currentChildStack.top();
			if ( nextChildRange._current == nextChildRange._end )
			{
				_currentChildStack.pop();
				continue;
			}
			if ( incrementNext )
				++nextChildRange._current;
			else
				break;
		}
		if ( _currentChildStack.empty() )
			Base<DerivedType,NodeType>::ClearRootNode(); // we're finished. this is now an "end" iterator
	}
	
	bool equal( const PreOrder& other ) const
	{
		if ( other.GetRootNode() == Base<DerivedType,NodeType>::GetRootNode() )
		{
			if ( _currentChildStack.empty() )
				return other._currentChildStack.empty();
			if ( !other._currentChildStack.empty() )
				return _currentChildStack.top() == other._currentChildStack.top();
		}
		return false;
	}
	
	NodeType& dereference() const
	{
		// if the root node is NULL, enjoy the surprises that await you
		return _currentChildStack.empty() ? *Base<DerivedType,NodeType>::GetRootNode() : *_currentChildStack.top()._current;
	}

	void increment()
	{
		// it's possible that root node is NULL.
		// but just as with doing a vector.begin()++, it's not our concern what happens
		
		if ( _currentChildStack.empty() ) {
			const TChildItHolder nextChildRange( static_cast<DerivedType*>(this)->GetChildRangeForNode( *Base<DerivedType,NodeType>::GetRootNode() ) );
			if ( nextChildRange._current == nextChildRange._end ) // is the tree empty except for the root node?
				Base<DerivedType,NodeType>::ClearRootNode();
			else
				_currentChildStack.push( nextChildRange );
		} else {
			TChildItHolder& currentChildRange = _currentChildStack.top();
			if ( currentChildRange._current == currentChildRange._end )
			{
				windBack( true );
			} else
			{
				TChildItHolder nextLowestRange( static_cast<DerivedType*>(this)->GetChildRangeForNode( *currentChildRange._current ) );
				++currentChildRange._current;
				if ( nextLowestRange._current != nextLowestRange._end )
					_currentChildStack.push( nextLowestRange );
				else
				{
						// reposition to the next available if necessary
					if ( currentChildRange._current == currentChildRange._end )
					{
						windBack( false );
					}
				}
			}
		}
	}
};


}